package com.guli.serviceBase.ExceptionHandler;

import com.guli.commonUtils.ExceptionUtil;
import com.guli.commonUtils.R;
import com.guli.serviceBase.Entity.GuliExcepiton;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author zxc
 * @date 2023/03/28 13:58
 **/
//全局异常处理
@ControllerAdvice // 对于全局数据进行处理
@ResponseBody
@Slf4j
public class GlobalExceptionHandler{

    // 处理自定义异常
    @ExceptionHandler(GuliExcepiton.class)
    public R error(GuliExcepiton e){
        log.error(ExceptionUtil.getMessage(e));
        e.printStackTrace();
        return R.error().message(e.getMsg()).code(e.getCode());
    }

    // 处理全局异常
    @ExceptionHandler(Exception.class)
    public R error(Exception e){
        log.error(ExceptionUtil.getMessage(e));
        e.printStackTrace();
        return R.error().message("执行了全局异常~~~");
    }

    // 处理特定异常
    @ExceptionHandler(ArithmeticException.class)
    public R error(ArithmeticException e){
        log.error(ExceptionUtil.getMessage(e));
        e.printStackTrace();
        return R.error().message("执行了特定异常~~~");
    }


}
